package com.zxl.live.screen.screen.constant;

import com.zxl.live.screen.screen.ui.service.ScreenService;
import com.zxl.live.tools.tools.SpUtils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

/**
 * 数据存储器
 * Created by makai on 15-6-15.
 */
public class SettingsDataSave {

	/**
	 * 初始化数据
	 * @param cls
	 */
	public static void initData(Class cls, String spName) {
		Field[] fields = cls.getFields();
		if (null != fields && fields.length > 0) {
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				Settings settings = field.getAnnotation(Settings.class);
				if (null != settings) {
					if (SpUtils.getSharedPreferences(spName).contains(settings.name())) {
						putData(spName, field, settings, settings.name());
					} else {
						try {
							saveData(cls, spName, settings.name(), field.get(null));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

	/**
	 * 保存数据
	 * @param name
	 * @param value
	 */
	public static void saveData(Class cls, String spName, String name, Object value) {
		Field[] fields = cls.getFields();
		if (null != fields && fields.length > 0) {
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				Settings settings = field.getAnnotation(Settings.class);
				if (null != settings) {
					if (settings.name().equals(name)) {
						saveData(spName, field, settings, name, value);
					}
				}
			}
		}
	}

	/**
	 * 保存数据
	 * @param field
	 * @param settings
	 * @param name
	 * @param value
	 */
	public static void saveData(String spName, Field field, Settings settings, String name, Object value) {
		if (null != value && settings.name().equals(name)) {
			Class<?> cls = value.getClass();
			if (cls.equals(String.class)) {
				try {
					SpUtils.getSharedPreferences(spName).edit().putString(name, value.toString()).commit();
					field.set(null, value.toString());
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (cls.equals(Integer.class) || cls.equals(int.class)) {
				try {
					int v = Integer.parseInt(value.toString());
					SpUtils.getSharedPreferences(spName).edit().putInt(name, v).commit();
					field.setInt(null, v);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (cls.equals(Boolean.class) || cls.equals(boolean.class)) {
				try {
					boolean v = Boolean.parseBoolean(value.toString());
					SpUtils.getSharedPreferences(spName).edit().putBoolean(name, v).commit();
					field.setBoolean(null, v);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (cls.equals(Float.class) || cls.equals(int.class)) {
				try {
					float v = Float.parseFloat(value.toString());
					SpUtils.getSharedPreferences(spName).edit().putFloat(name, v).commit();
					field.setFloat(null, v);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (cls.equals(Long.class) || cls.equals(long.class)) {
				try {
					long v = Long.parseLong(value.toString());
					SpUtils.getSharedPreferences(spName).edit().putLong(name, v).commit();
					field.setLong(null, v);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else {
				throw new RuntimeException("not support now");
			}
		}
	}

	/**
	 * 设置数据
	 * @param field
	 * @param settings
	 * @param name
	 */
	public static void putData(String spName, Field field, Settings settings, String name) {
		if (settings.name().equals(name)) {
			Class clas = field.getType();
			if (clas.equals(String.class)) {
				try {
					field.set(null, SpUtils.getSharedPreferences(spName).getString(name, ""));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (clas.equals(Integer.class) || clas.equals(int.class)) {
				try {
					field.setInt(null, SpUtils.getSharedPreferences(spName).getInt(name, 0));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (clas.equals(Boolean.class) || clas.equals(boolean.class)) {
				try {
					field.setBoolean(null, SpUtils.getSharedPreferences(spName).getBoolean(name, false));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (clas.equals(Float.class) || clas.equals(float.class)) {
				try {
					field.setFloat(null, SpUtils.getSharedPreferences(spName).getFloat(name, 0));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else if (clas.equals(Long.class) || clas.equals(long.class)) {
				try {
					field.setLong(null, SpUtils.getSharedPreferences(spName).getLong(name, 0));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else {
				throw new RuntimeException("not support now : " + clas + ", " + field.getName());
			}
		}
	}

	/**
	 * 设置属性
	 */
	@Target(ElementType.FIELD)
	@Retention(RetentionPolicy.RUNTIME)
	public @interface Settings {

		String name();

		Class cls() default ScreenService.class;

	}

}
